﻿#ifndef SERIALPORTCONTROLLER_H
#define SERIALPORTCONTROLLER_H

#include <QObject>

#include "GlobalDef.h"
#include "MessDispath.h"

#include "Serialport/serialportconfig.h"
#include "Serialport/asyncserialport.h"
#include "Serialport/serialport.h"

namespace Core {

	class SerialPortController : public QObject
	{
		Q_OBJECT

	public:
		explicit SerialPortController(QObject *parent = nullptr);
		~SerialPortController();

		/**
		 * @brief  getSerialPortList 获取串口列表
		 * @return 
		 */
		QStringList getSerialPortList();

		/**
		 * @brief	setOpenSerialPort 设置打开串口
		 * @param	strPortName 串口名称
		 * @param	iBaudRateIndex 串口波特率
		 * @param	iDataBitsIndex 串口数据位
		 * @param	iParityIndex
		 * @param	iStopBitsIndex 串口停止位
		 * @param	iFlowControlIndex 串口流控
		 * @return 
		 */
		bool setOpenSerialPort(QString strPortName, int iBaudRateIndex, int iDataBitsIndex, int iParityIndex, int iStopBitsIndex, int iFlowControlIndex);

		/**
		 * @brief	setCloseSerialPort 设置关闭串口
		 * @return 
		 */
		void setCloseSerialPort();

		/**
		 * @brief	getSerialPortIsOpen  获取串口是否打开
		 * @return 
		 */
		bool getSerialPortIsOpen();

		/**
		 * @brief	setSendSerialMsg 设置发送的串口消息
		 * @param	strSendMsg 串口消息
		 * @return 
		 */
		void setSendSerialMsg(const QString strSendMsg);

		/**
		 * @brief	setSendMsgType 
		 * @param	iType
		 * @return 
		 */
		void setSendMsgType(int iType);

		/**
		 * @brief	setRecvMsgType
		 * @param	iType
		 * @return 
		 */
		void setRecvMsgType(int iType);

		/**
		 * @brief	setClearMsgLength 设置清除信息长度
		 * @return 
		 */
		void setClearMsgLength();

	Q_SIGNALS:
		/**
		 * @brief	signalSerialPortMsgInfo
		 * @param	iMsgType
		 * @param	strMsg
		 * @return 
		 */
		void signalSerialPortMsgInfo(int iMsgType, QString strMsg);

	private Q_SLOTS: 
		/**
		 * @brief	slotRecvSerialMsg 接收串口信息
		 * @param	buff 串口信息
		 * @return 
		 */
		void slotRecvSerialMsg(QByteArray buff);

	private:
		QString ByteArrayToHexString(QByteArray data);

		QByteArray hexStringToByteArray(QString hexString);

	private:
		Common::AsyncSerialPort *m_pAsyncSerialPort;	// 串口指针
		int m_iSendMsgType;								// 
		int m_iRecvMsgType;								//
		int m_iSendMsgTotalLength;						//
		int m_iRecvMsgTotalLength;						//
	};

}//namespace Core 

#endif // SERIALPORTCONTROLLER_H